//
//  WriteViewController.m
//  UI_通讯_26
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 liwenhan. All rights reserved.
//

#import "WriteViewController.h"
#import "NewView.h"
#import "CoreDataManager.h"
#import "Persen.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface WriteViewController ()

@property (nonatomic, strong)NewView *addNewView;

@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong)UIButton *leftButton;
@property (nonatomic, strong)UIButton *rightButton;

@property (nonatomic, strong)UIView *naviView;

@property (nonatomic, strong)CoreDataManager *manager;


@end

@implementation WriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建数据管理器(单例)
    self.manager = [CoreDataManager defaults];
    NSLog(@"url ====  %@", [self.manager applicationDocumentsDirectory]);
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.naviView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    [self.view addSubview:self.naviView];
    
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.naviView addSubview:self.leftButton];
    self.leftButton.frame = CGRectMake(0, 20, 60, 44);
    [self.leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:
     UIControlEventTouchUpInside];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.naviView addSubview:self.rightButton];
    self.rightButton.frame = CGRectMake(WIDTH - 60, 20, 60, 44);
    [self.rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.addNewView = [[NewView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
    [self.view addSubview:self.addNewView];

    // 赋值
    self.addNewView.nameField.text = self.twoChuanPerson.name;
    self.addNewView.phoneField.text = self.twoChuanPerson.phone;
    self.addNewView.numberField.text = self.twoChuanPerson.number;
    self.addNewView.cityField.text = self.twoChuanPerson.city;
    self.addNewView.qqField.text = self.twoChuanPerson.qq;
    

    
}

//  取消按钮
- (void)leftButtonAction:(UIButton *)button
{
    //返回给上一页
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//  保存按钮
- (void)rightButtonAction:(UIButton *)button
{
    // 获取查询数据的请求, 相当于SQL的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Persen"];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    Persen *person = array[self.j];
    person.name = self.addNewView.nameField.text;
    person.number = self.addNewView.numberField.text;
    person.phone = self.addNewView.phoneField.text;
    person.qq = self.addNewView.qqField.text;
    person.city = self.addNewView.cityField.text;
    
    [self.manager saveContext];
    
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
